/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-20
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * @author springyounger
 *
 */
public class Encrypt {
    public static String encodeMD5(String str) {
        if (null==str) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest code = MessageDigest.getInstance("MD5");
            code.update(str.getBytes());
            byte[] bs = code.digest();
            for (int i = 0; i < bs.length; i++) {
                int v = bs[i] & 0xFF;
                if (v < 16) {
                    sb.append(0);
                }
                sb.append(Integer.toHexString(v));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString().toUpperCase();
    }

    public static boolean isValidInput(String str) {
        return str.matches("[a-z0-9]+");
    }
    
    public static void main(String args[]){
        System.out.println(Encrypt.encodeMD5("admin"));
        System.out.println(Encrypt.encodeMD5("test"));
    }
}
